﻿namespace TianYu.Core.Common
{
    /// <summary>
    /// 通用分页信息类
    /// </summary>
    public class PageModel<T>
    {
        /// <summary>
        /// 当前页标
        /// </summary>
        public int page { get; set; } = 1;
        /// <summary>
        /// 总页数
        /// </summary>
        public int pageCount { get { return (Math.Ceiling(dataCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt(); } }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int dataCount { get; set; } = 0;
        /// <summary>
        /// 每页大小
        /// </summary>
        public int pageSize { set; get; } = 20;
        /// <summary>
        /// 返回数据
        /// </summary>
        public List<T> data { get; set; }
    }

    public static class PageModelExtensions
    {
        public static PageModel<T> ToPageModel<T>(this List<T> @this, int page, int pageSize, int totalCount = 0)
        {
            return new PageModel<T>() { dataCount = totalCount, page = page, pageSize = pageSize, data = @this };
        }
    }
}
